package com.example.doudou.demos.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.domain.R;
import com.example.doudou.demos.pojo.Products;
import com.example.doudou.demos.pojo.UserLogRecord;
import com.example.doudou.demos.service.ProductsService;
import com.example.doudou.demos.mapper.ProductsMapper;
import com.example.doudou.demos.utils.RedisUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
* @author fangtao
* @description 针对表【products(商品信息表)】的数据库操作Service实现
* @createDate 2025-06-03 16:52:00
*/
@Service
@Slf4j
@AllArgsConstructor
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products>
    implements ProductsService{
    private final ProductsMapper productsMapper;
    private final RedisUtils redisUtils;
    private final RedisLockTest redisLockTest;
    private final RedissonClient redissonClient;

    @Resource(name = "getDataHandleExecutor")
    private ThreadPoolTaskExecutor executorService;
    // 产品库存lock前缀
    private final String productLockKeyPrefix = "PRODUCT_LOCK_KEY:";
    @Override
//    public synchronized  R<String> shopping() {
    public   R<String> shopping() {
        Integer num=0;
        Products products=new Products();
        //将商品数量存储在redis中进行处理
        if(redisUtils.hasKey("stock")){
            num= (Integer) redisUtils.get("stock");
        }else {
            //获取商品信息
             products = productsMapper.selectById(1);
            //获取库存数量
            num=products.getStockQuantity();
            redisUtils.set("stock",num,60);

        }
         if(num>0){
             num--;
             redisUtils.set("stock",num,60);
             products.setStockQuantity(num);
             products.setId(1);
             productsMapper.updateById(products);
         }else {
             return R.error("库存不足");
         }

        return R.ok("扣减成功,还剩"+num+"个");
    }

    @Override
    public R<String> shopping2() throws InterruptedException {
        redisLockTest.t1();
        return R.ok("扣减成功");
    }

    @Override
    public R<String> redissonLock() {
        //
        RLock lock = redissonClient.getLock("myLock");
        try {
            // 尝试加锁，最多等待100秒，上锁后10秒自动解锁
            boolean isLocked = lock.tryLock(100, 10, TimeUnit.SECONDS);
            if (isLocked) {
               return shopping();
            }else {
                return R.error("获取锁失败");
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            // 释放锁
            lock.unlock();
        }

    }
}




